//
//  ProgressImageView.swift
//  Weibo
//
//  Created by apple on 15/12/22.
//  Copyright © 2015年 idcast. All rights reserved.
//

import UIKit
///带进度的图像视图
///面试题： 如果在UIImageView中的drawRect中绘图会怎么样？ 不会执行drawRect方法

class ProgressImageView: UIImageView {
    
    ///外部传递的进度值
    var progress:CGFloat = 0 {
    
        didSet{
        
            progressView.progress = progress
        }
    }

    //MARK: 构造函数
    //一旦给构造函数指定了参数 系统不在提供默认的构造函数
    init() {
    
        super.init(frame: CGRectZero)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        //1 添加控件
        addSubview(progressView)
        progressView.backgroundColor = UIColor.clearColor()
        
        //设置布局
        progressView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.snp_edges)
        }
        
    }
   ///懒加载控件
    private lazy var progressView:ProgressView = ProgressView()

}
///进度试图
private class ProgressView: UIView {
    
    ///内部使用的进度值
    var progress:CGFloat = 0 {
        
        didSet{
            
            //重绘试图
            setNeedsDisplay()
        }
    }

    //rect 就等于 bounds
    override func drawRect(rect: CGRect) {
        let center = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
        let r = min(rect.width, rect.height) * 0.5
        let start = CGFloat(-M_PI_2)
        let end = start + progress * 2 * CGFloat(M_PI)
        /**
         1 中心点
         2 半径
         3 起始弧度
         4 截止弧度
         5 是否顺时针
         
         */
        let path = UIBezierPath(arcCenter: center, radius: r, startAngle: start, endAngle: end, clockwise: true)
        //添加到中心点的连线
        path.addLineToPoint(center)
        path.closePath()

        UIColor(white: 1.0, alpha: 0.3).setFill()
        
        path.fill()
        
    }
    
}